'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:IndexIndexCtrl
 * @description
 * # IndexIndexCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('IndexIndexCtrl', function($scope, $location, $http, openApi, Modulemap, Util, localStorageService) {
        $scope.msg = 11

        // 跳转到相应模块
        $scope.gosection = function(item) {
                localStorageService.set("sublist", item);
                // 如果有id说明直接进入子项目，没有id直接进入二级列表
                if (item.id) {
                    if (!item.viewName) {
                        Util.Toast("移动端暂不支持该流程的发起，请您在电脑上完成该操作");
                        return
                    }
                    //  用于自定义字段参数
                    localStorageService.set("iconUrl", item.iconUrl);
                    $location.path(item.viewName + "/" + item.id)
                } else {
                    $location.path("index/subindex")
                }
            }
            // 过滤数据
        var getCompleteData = function(data) {
            var dataObj = {
                dataArr: [],
                datalength: 0
            };
            data.forEach(function(item, index) {
                //计算二级分类长度，如果大于八则显示一级分类，否则直接显示二级分类
                //item表示一级分类，subitem表示二级分类
                dataObj.datalength += item.subcategory.length;
                //为一级分类添加viewname以及className
                var itemViewNameObj = {};
                itemViewNameObj = Modulemap.getViewNameAndIcon(item.categoryType);
                item.viewName = itemViewNameObj.viewName;
                item.className = itemViewNameObj.className;
                item.intro = itemViewNameObj.intro;
                item.subcategory.forEach(function(subitem, index) {
                    var subitemViewNameObj = {};
                    //根据iconUrl添加二级分类viewname以及className
                    subitemViewNameObj = Modulemap.getViewNameAndIcon(subitem.iconUrl);
                    subitem.viewName = subitemViewNameObj && subitemViewNameObj.viewName;
                    subitem.className = subitemViewNameObj && subitemViewNameObj.className;
                    dataObj.dataArr.push(subitem);
                })

            });
            return dataObj
        }
        var getDatalist = function(data) {
            var datalist = []
            var dataObj;
            dataObj = getCompleteData(data);
            if (dataObj.datalength > 8) {
                datalist = data
            } else {
                datalist = dataObj.dataArr
            }
            return datalist

        }
        var getmenulist = function() {
            $http.get(openApi.category).success(function(data, status, headers, config) {
                if (data.logout) {
                    Util.logout();
                    return;
                }
                if (data.success) {
                    if (Util.isNullBbj(data.result)) {
                        Util.Toast("当前账号没有数据");
                        return;
                    } else {
                        var datalist = getDatalist(data.result)
                        $scope.menulist = datalist
                    }

                }
            }).error(function(data, status, headers, config) {
                // Util.Toast("no data!");
                console.log(headers)

            });
        };
        var init = function() {
            getmenulist()
            Util.checkVersion();
        }
        init()
    });
